<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :immediate-check="false"
        @load="onLoad"
      >
        <!-- 文章列表 -->
        <ArticleItem
          v-for="obj in list"
          :key="obj.art_id"
          :artObj="obj"
          offset="50"
        ></ArticleItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import ArticleItem from './ChannelEdit.vue'
import { getArticleNewsAPI } from '@/api'
export default {
  props: {
    channelId: Number // 频道ID
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      timestamp: new Date().getTime(),
      isLoading: false
    }
  },
  components: {
    ArticleItem
  },
  created () {
    this.ArticleNewsAPI()
  },
  methods: {
    async ArticleNewsAPI () {
      const res = await getArticleNewsAPI({
        channel_id: this.channelId,
        timestamp: this.timestamp
      })
      if (res.data.data.results === 0) {
        this.finished = true
        return
      }
      this.list = [...this.list, ...res.data.data.results]
      this.timestamp = res.data.data.pre_timestamp
      this.loading = false
      // 加载结束没有数据解决400报错
      if (res.data.data.pre_timestamp === null) {
        this.finished = true
      }
    },
    onLoad () {
      if (this.list.length > 0) {
        this.ArticleNewsAPI()
      }
    },
    onRefresh () {
      this.list = []
      this.timestamp = new Date().getTime()
      this.ArticleNewsAPI()
      setTimeout(() => {
        this.$toast.success('刷新成功')
        this.isLoading = false
      }, 1000)
    }
  }
}
</script>
